
/*

*/

(function($){

var 
	getType = function(me){return me.find(">fieldset>select[name=type]");},
	getCloud = function(me){return me.find(">fieldset>div.tag_cloud");},
	getTabber = function(me){return me.find(">div.tabber:first");},
	getSelect = function(me){return getTabber(me).find(">div:eq(0)>select");},
	getButtonNew = function(me){return getTabber(me).find(">div:eq(1)>button");},
	getButtonSelect = function(me){return getTabber(me).find(">div:eq(0)>button");},
	getHidden = function(me){return me.find(">input[type=hidden]:first");},

	populateAtoms = function($this, atom_id){
		var	i,data = {
				action: "list",
				type:getType($this).selectedVal(),
				tags:getCloud($this).tagCloudGet(),
				};
		$this.one("am_populated", function(e,ret,err,html){
				if ( !err ) {
					for (i in ret.atoms ){
						getSelect($this).empty().addOption( i, "["+ret.atoms[i].type + "] "+ret.atoms[i].title );
					}
				}
				getSelect($this).selectValue( atom_id );
				return false;
			});
		$this.jsonRequest("MgmBoxes", data, "am_populated");
		return false;
	},

	createNew = function(e){
		var $this = e.data,
			data = {
				action:"create",
				type:getType($this).selectedVal(),
				title:getTabber($this).find(">div:eq(1) input[type=text]").val(),
				tags:getCloud($this).tagCloudGet(),
				};
		$this.one("am_created", function(e,data,err,html){
				if ( !err ) {
					populateAtoms($this, data.atom_id );
					getTabber($this).tabs("select", 0);
				}
				return false;
			});
		$this.jsonRequest("BoxRequest", data, "am_created");
		return false;
	},

	eventUpdate = function(e){
		populateAtoms(e.data);
		return false;
	},

	saveAtom = function(e){
		getHidden(e.data).val( getSelect(e.data).selectedVal() );
	},

	triggerSelect = function(e){
		var $this = e.data;
		$this.trigger("atom_selected",
					[getSelect($this).selectedVal(),
					getSelect($this).selectedText()] );
		return false;
	};


Core.ObjectTypes["div.atom_manager"] = function(){
	var $this = $(this);
	var atom_id = getHidden($this).val();
	if ( atom_id ){
		$this.atomManagerSet(atom_id);
	}

	getType($this).bind("change", $this, eventUpdate);
	getCloud($this).bind("cloud_update", $this, eventUpdate);

	getSelect($this).bind("change", $this, saveAtom);
	getButtonSelect($this).bind("click", $this, triggerSelect);
	getButtonNew($this).bind("click", $this, createNew);
};

$.fn.atomManagerSet = function(atom_id){

	$(this).one("am_initialize", $(this), function(e,data,err,html,req){
				var x;
				var $this = e.data;
				if ( !err ){
					getCloud($this).cloudInhibitUpdate();
					getCloud($this).tagClear();
					for (x in data["tag_cloud"]){
						getCloud($this).tagSelect( data["tag_cloud"][x] );
					}
					getCloud($this).cloudAllowUpdate();
					getType($this).selectValue(data["type"]);
					populateAtoms($this, atom_id);
				}
				return false;
			});


	$(this).jsonRequest("MgmBoxes",{ 'action':"get",
										'id':atom_id,
										'type':true,
										'tag_cloud':true}, "am_initialize");
};

}(jQuery));
